package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcEmrEntity;
import com.dhcc.cdc.busi.service.ICdcEmrService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 电子病历
 * 
 * @ClassName: CdcEmrController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:08
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcemr")
public class CdcEmrController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcemr/";

	@Autowired
	private ICdcEmrService cdcEmrService;
	

    /**
     * 跳转到电子病历列表首页
     */
    @RequestMapping("")
    @Permission("cdcemr:list")
    public String index() {
        return PREFIX + "cdcemr.html";
    }
	
    /**
     * 跳转到新增电子病历
     */
    @RequestMapping("/to_add")
    @Permission("cdcemr:add")
    public String toAdd() {
        return PREFIX + "cdcemr_add.html";
    }
	
	/**
     * 跳转到修改电子病历
     */
    @RequestMapping("/to_update/{cdcEmrId}")
    @Permission("cdcemr:update")
    public String toUpdate(@PathVariable Long cdcEmrId, Model model) {
        CdcEmrEntity cdcEmr = this.cdcEmrService.selectById(cdcEmrId);
        model.addAttribute("cdcEmr",cdcEmr);
        return PREFIX + "cdcemr_edit.html";
    }

    /**
     * 获取电子病历列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcemr:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcEmrEntity> page = new PageFactory<CdcEmrEntity>().defaultPage();
        List<CdcEmrEntity> result = this.cdcEmrService.page(page,params);
        page.setRecords((List<CdcEmrEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增电子病历
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcemr:add")
    @BussinessLog(value = "新增电子病历")
    public Object add(CdcEmrEntity cdcEmr) {
        if (CommonUtil.isOneEmpty(cdcEmr)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcEmr.insert();
        return R.ok();
    }

    /**
     * 删除电子病历
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcemr:delete")
    @BussinessLog(value = "删除电子病历")
    public Object delete(@RequestParam Long cdcEmrId) {
	
        this.cdcEmrService.deleteById(cdcEmrId);

        return R.ok();
    }

    /**
     * 修改电子病历
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcemr:update")
    @BussinessLog(value = "修改电子病历")
    public Object update(CdcEmrEntity cdcEmr) {
        if (CommonUtil.isOneEmpty(cdcEmr)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcEmrEntity old = this.cdcEmrService.selectById(cdcEmr.getId());

        old.setEpisodeId(cdcEmr.getEpisodeId());
        old.setXcode(cdcEmr.getXcode());
        old.setBusEmrTypeId(cdcEmr.getBusEmrTypeId());
        old.setTitle(cdcEmr.getTitle());
        old.setSubTitle(cdcEmr.getSubTitle());
        old.setContent(cdcEmr.getContent());
        old.setEmrDate(cdcEmr.getEmrDate());
        old.setEmrUserId(cdcEmr.getEmrUserId());
        old.setDataId(cdcEmr.getDataId());
        old.setIsActive(cdcEmr.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
